package com.qf.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsGoodsMapper;
import com.qf.mapper.StatMapper;
import com.qf.pojo.DtsGoods;
import com.qf.pojo.DtsGoodsAttribute;
import com.qf.pojo.DtsGoodsProduct;
import com.qf.pojo.DtsGoodsSpecification;
import com.qf.service.DtsGoodsAttributeService;
import com.qf.service.DtsGoodsProductService;
import com.qf.service.DtsGoodsService;
import com.qf.service.DtsGoodsSpecificationService;
import com.qf.vo.DayStatis;
import com.qf.vo.GoodsAllinone;
import com.qf.vo.StatGoods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DtsGoodsServiceImpl implements DtsGoodsService {
    @Autowired
    DtsGoodsMapper goodsMapper;
    @Autowired
    StatMapper statMapper;
    @Override
    public int getTotal() {
        LambdaQueryWrapper<DtsGoods> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsGoods::getDeleted,false);
        Integer count = goodsMapper.selectCount(wrapper);
        return count;
    }
    @Override
    public List<Map<String, Object>> findCategoryShellData() {
        return statMapper.findCategoryShellData();
    }

    @Override
    public List<DayStatis> findGoodsAmts() {
        return statMapper.findGoodsAmts();
    }

    @Override
    public Map<String, Object> queryPage(Integer page, Integer limit, String goodsSn, String name, String sort, String order, List<Integer> brandIds) {
        IPage<DtsGoods> ipage = new Page<>(page, limit);

        QueryWrapper<DtsGoods> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(goodsSn)) {
            wrapper.like("goods_sn", goodsSn);
        }
        if(!StringUtils.isEmpty(name)) {
            wrapper.like("name", name);
        }

        if(!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)){
            if("desc".equals(order)){
                wrapper.orderByDesc(sort);
            }else{
                wrapper.orderByAsc(sort);
            }
        }
        //如果有 品牌id
        if(brandIds != null && brandIds.size() > 0){
            wrapper.in("brand_id" , brandIds);
        }
        goodsMapper.selectPage(ipage, wrapper);
        Map<String,Object> data = new HashMap<>();
        data.put("total", ipage.getTotal());
        data.put("items", ipage.getRecords());
        return data;
    }
    @Override
    public DtsGoods selectById(Integer id) {
        DtsGoods goods = goodsMapper.selectById(id);
        return goods;
    }
    @Autowired
    DtsGoodsAttributeService attributeService;
    @Autowired
    DtsGoodsSpecificationService specificationService;
    @Autowired
    DtsGoodsProductService productService;
    @Override
    public void update(GoodsAllinone goodsAllinone) {
        //获取商品信息
        DtsGoods goods = goodsAllinone.getGoods();
        goods.setUpdateTime(new Date());

        //更新商品数据
        goodsMapper.updateById(goods);
        //更新商品的属性
        //删除该商品的属性
        attributeService.deleteByGoodsId(goods.getId());
        //新增商品的属性
        DtsGoodsAttribute[] attributes = goodsAllinone.getAttributes();
        if(attributes != null && attributes.length > 0){
            for (DtsGoodsAttribute attribute : attributes) {
                //设置商品id
                attribute.setGoodsId(goods.getId());
                attributeService.insert(attribute);
            }
        }
        //更新商品的规格
        //删除该商品的规格
        specificationService.deleteByGoodsId(goods.getId());
        //新增商品的规格
        DtsGoodsSpecification[] specifications = goodsAllinone.getSpecifications();
        if(specifications != null && specifications.length > 0){
            for (DtsGoodsSpecification specification : specifications) {
                //设置商品id
                specification.setGoodsId(goods.getId());
                specificationService.insert(specification);
            }
        }
        //更新商品的库存
        //删除该商品的库存
        productService.deleteByGoodsId(goods.getId());
        //新增商品的库存
        DtsGoodsProduct[] products = goodsAllinone.getProducts();
        if(products != null && products.length > 0){
            for (DtsGoodsProduct product : products) {
                product.setGoodsId(goods.getId());
                productService.insert(product);
            }
        }
    }
    @Override
    public void save(GoodsAllinone goodsAllinone) {
        //保存商品信息
        DtsGoods goods = goodsAllinone.getGoods();
        goods.setAddTime(new Date());
        goods.setUpdateTime(new Date());
        goods.setDeleted(false);

        goodsMapper.insert(goods);

        //新增商品的属性
        DtsGoodsAttribute[] attributes = goodsAllinone.getAttributes();
        if(attributes != null && attributes.length > 0){
            for (DtsGoodsAttribute attribute : attributes) {
                //设置商品id
                attribute.setGoodsId(goods.getId());
                attributeService.insert(attribute);
            }
        }
        //新增商品的规格
        DtsGoodsSpecification[] specifications = goodsAllinone.getSpecifications();
        if(specifications != null && specifications.length > 0){
            for (DtsGoodsSpecification specification : specifications) {
                //设置商品id
                specification.setGoodsId(goods.getId());
                specificationService.insert(specification);
            }
        }
        //新增商品的库存
        DtsGoodsProduct[] products = goodsAllinone.getProducts();
        if(products != null && products.length > 0){
            for (DtsGoodsProduct product : products) {
                product.setGoodsId(goods.getId());
                productService.insert(product);
            }
        }
    }

    @Override
    public List<StatGoods> findAllGoodsStatis() {
        return statMapper.findAllGoodsStatis();
    }

    @Override
    public void delete(DtsGoods goods) {
        attributeService.updateByGoodsId(goods.getId());
        specificationService.updateByGoodsId(goods.getId());
        productService.updateByGoodsId(goods.getId());
        //设置为逻辑删除
        goods.setDeleted(true);
        goodsMapper.updateById(goods);
    }
}
